package com.wjzjq.hiring.talent.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wjzjq.hiring.common.DBObject;
import com.wjzjq.hiring.common.util.DBUtil;
import com.wjzjq.hiring.talent.entity.ResumeEntity;

public class ResumeDao {
	public static ResumeEntity getResume(int id) throws SQLException {
		String sql = "select * from resume where id=?";
		DBObject dbObject = DBUtil.executeQuery(sql, id);
		ResultSet rs = dbObject.getResultSet();
		ResumeEntity resume = null;
		if (rs.next()) {
			resume = new ResumeEntity();
			resume.setIntention(rs.getString("intention"));
			resume.setProjectExperience(rs.getString("project_experience"));
			resume.setWorkExperience(rs.getString("work_experience"));
			resume.setId(id);
		}
		return resume;
	}

	public static int updateResume(int id, ResumeEntity r) {
		String sql = "update resume set intention=?,work_experience=?,project_experience=? where id=?";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(r.getIntention());
		paramList.add(r.getWorkExperience());
		paramList.add(r.getProjectExperience());
		paramList.add(id);
		int row = DBUtil.executeUpdate(sql, paramList);
		return row;

	}

	public static List<ResumeEntity> getResumeList(int id) {
		List<ResumeEntity> resumeList = new ArrayList<ResumeEntity>();
		String sql = "select id,intention,work_experience,project_experience from resume where talent_id =?";
		DBObject dbObject = DBUtil.executeQuery(sql, id);
		ResultSet resultSet = dbObject.getResultSet();
		try {
			while (resultSet.next()) {
				ResumeEntity resume = new ResumeEntity();
				resume.setId(resultSet.getInt("id"));
				resume.setIntention(resultSet.getString("intention"));
				resume.setProjectExperience(resultSet
						.getString("project_experience"));
				resume.setWorkExperience(resultSet.getString("work_experience"));
				resumeList.add(resume);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resumeList;

	}

	public int delete(int id) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM resume WHERE id = ?";
		int row = DBUtil.executeUpdate(sql, id);
		return row;
	}

	public static int addResume(ResumeEntity resume) {
		// TODO Auto-generated method stub
		int row = 0;
		int talentId = resume.getTalentId();
		String intention = resume.getIntention();
		String workExperience = resume.getWorkExperience();
		String projectExperience = resume.getProjectExperience();
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(talentId);
		paramList.add(intention);
		paramList.add(workExperience);
		paramList.add(projectExperience);

		String sql = "insert into resume values(null,?,?,?,?)";
		row = DBUtil.executeUpdate(sql, paramList);
		return row;
	}

	/**
	 * 批量删除 事务
	 * 
	 * @param deleteIds
	 * @return
	 */
	public static void main(String[] args) {
		ResumeDao r = new ResumeDao();
		int[]ints = new int[]{8};
		System.out.println(r.deleteByIds(ints));
	}
	public boolean deleteByIds(int[] deleteIds) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = null;
		PreparedStatement pstm = null;
		String sql = "delete from resume where id=?";
		try {
			conn = DBUtil.getConnection();
			conn.setAutoCommit(false);
			pstm = conn.prepareStatement(sql);
			for (int i = 0; i < deleteIds.length; i++) {
				pstm.setInt(1, deleteIds[i]);
				pstm.addBatch();
			}
			pstm.executeBatch();
			conn.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			DBUtil.close(pstm, conn);
		}
		return flag;
	}

}
